/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.expression;

import java.util.Collections;
import java.util.List;

import com.prefabware.jmodel.JType;
import com.prefabware.jmodel.code.CodeBuffer;
import com.prefabware.jmodel.code.JCodeOptions;

public class JVariableDeclarationBase implements JVariableDeclaration {
	private boolean isFinal;
	private String name;
	private JType type;
	private List<JType> typeArguments;

	@Override
	public List<JType> getTypeArguments	() {
		return typeArguments;
	}

	public JVariableDeclarationBase(boolean isFinal, String name, JType type,
			List<JType> typeParameters) {
		super();
		this.isFinal = isFinal;
		this.name = name;
		this.type = type;
		if (typeParameters == null) {
			this.typeArguments = Collections.emptyList();
		} else {
			this.typeArguments = typeParameters;
		}
	}

	@Override
	public boolean isFinal() {
		return isFinal;
	}

	@Override
	public String getName() {
		return name;
	}

	public JType getType() {
		return type;
	}

	@Override
	public String asCode(JCodeOptions options) {
		CodeBuffer buf = new CodeBuffer(options);
		if (isFinal) {
			buf.append("final");			
		}
		buf.append(this.getType());
		String typeArgumentsCsv = options.toCsv(this.getTypeArguments());
		buf.setInsertLeadingSpace(false);
		buf.appendIfNotNullOrEmpty(typeArgumentsCsv, "<",typeArgumentsCsv,">");
		//in the java declaration allways use the simple name of the variable
		buf.setInsertLeadingSpace(true);
		buf.append(this.getName());
		return buf.toString();
	}
}
